<template>
  <div class="min-h-screen bg-gradient-to-br from-gray-50 via-blue-50 to-indigo-50">
    <!-- 背景装饰 -->
    <div class="fixed inset-0 overflow-hidden pointer-events-none">
      <div class="absolute -top-40 -right-40 w-80 h-80 bg-blue-200 rounded-full mix-blend-multiply filter blur-xl opacity-20 animate-blob"></div>
      <div class="absolute -bottom-40 -left-40 w-80 h-80 bg-purple-200 rounded-full mix-blend-multiply filter blur-xl opacity-20 animate-blob animation-delay-2000"></div>
      <div class="absolute top-40 left-40 w-80 h-80 bg-pink-200 rounded-full mix-blend-multiply filter blur-xl opacity-20 animate-blob animation-delay-4000"></div>
    </div>

    <!-- 主要内容区域 -->
    <div class="container mx-auto px-12 py-10 pt-32 relative z-10">
      <!-- 顶部：返回与标题 -->
      <div class="flex items-center justify-between mb-10">
        <div class="flex items-center gap-4">
          <el-button type="primary" link @click="goBack" class="text-gray-600 hover:text-gray-800 text-lg">
            <el-icon class="mr-2 text-xl">
              <ArrowLeft />
            </el-icon>
            返回班级详情
          </el-button>
          <h1 class="text-4xl font-bold text-gray-900">知识点</h1>
        </div>
      </div>

      <!-- 主体内容 -->
      <div class="bg-white rounded-3xl shadow-xl border border-gray-200 overflow-hidden">
        <div class="px-8 py-6 border-b border-gray-200 bg-gradient-to-r from-blue-50 to-indigo-50">
          <div class="flex items-center justify-between">
            <div class="flex items-center gap-4">
              <div class="w-16 h-16 bg-gradient-to-br from-blue-500 to-indigo-600 rounded-2xl flex items-center justify-center">
                <el-icon class="text-white text-2xl">
                  <Reading />
                </el-icon>
              </div>
              <div>
                <h3 class="text-3xl font-bold text-gray-900 mb-2">班级知识点</h3>
                <p class="text-blue-600 font-medium text-xl">管理和查看班级相关知识点</p>
              </div>
            </div>
            <el-button type="primary" size="large" @click="showAddKnowledgePointDialog = true" class="rounded-full px-6 bg-gradient-to-r from-blue-500 to-indigo-600 hover:from-blue-600 hover:to-indigo-700 transition-all">
              <el-icon class="mr-2">
                <Plus />
              </el-icon>发布知识点
            </el-button>
          </div>
        </div>

        <!-- 知识点列表 -->
        <div class="p-8">
          <div v-if="knowledgePoints.length === 0" class="text-center py-20">
            <div class="w-24 h-24 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-6">
              <el-icon class="text-gray-400 text-4xl">
                <Document />
              </el-icon>
            </div>
            <h3 class="text-xl font-semibold text-gray-500 mb-4">暂无知识点</h3>
            <p class="text-gray-400 mb-6">点击上方"发布知识点"按钮添加班级知识点</p>
          </div>

          <div v-else class="space-y-6">
            <div v-for="point in knowledgePoints" :key="point.id" class="border border-gray-200 rounded-2xl p-6 hover:shadow-lg transition-shadow">
              <div class="flex justify-between items-start mb-4">
                <h4 class="text-2xl font-bold text-gray-900">{{ point.title }}</h4>
                <div class="flex gap-2">
                  <el-button type="text" size="small" @click="handleEdit(point)" class="text-blue-600">
                    <el-icon class="mr-1">
                      <Edit />
                    </el-icon>编辑
                  </el-button>
                  <el-button type="text" size="small" @click="handleDelete(point.id)" class="text-red-600">
                    <el-icon class="mr-1">
                      <Delete />
                    </el-icon>删除
                  </el-button>
                </div>
              </div>
              
              <div class="text-gray-700 mb-6 leading-relaxed">{{ point.content }}</div>
              
              <div class="flex flex-wrap gap-3 mb-6">
                <span v-for="tag in point.tags" :key="tag" class="bg-blue-100 text-blue-800 text-sm px-3 py-1 rounded-full">{{ tag }}</span>
              </div>
              
              <div class="flex justify-between items-center text-gray-500 text-sm">
                <span>发布于 {{ formatDate(point.createTime) }}</span>
                <el-button type="text" @click="handleViewDetails(point)" class="text-blue-600">
                  查看详情 <el-icon class="ml-1">
                    <ArrowRight />
                  </el-icon>
                </el-button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 添加/编辑知识点弹窗 -->
    <el-dialog 
      v-model="showAddKnowledgePointDialog" 
      title="发布知识点" 
      :width="600"
      :before-close="handleDialogClose"
    >
      <el-form :model="currentKnowledgePoint" :rules="formRules" ref="knowledgePointForm" label-width="100px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="currentKnowledgePoint.title" placeholder="请输入知识点标题" />
        </el-form-item>
        
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="currentKnowledgePoint.content"
            type="textarea"
            rows="6"
            placeholder="请输入知识点详细内容"
          />
        </el-form-item>
        
        <el-form-item label="标签">
          <el-input 
            v-model="currentTags" 
            placeholder="输入标签，用逗号分隔" 
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="showAddKnowledgePointDialog = false">取消</el-button>
        <el-button type="primary" @click="saveKnowledgePoint">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, toRefs } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowLeft, Reading, Plus, Edit, Delete, Document } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const classId = ref(route.params.id)

// 班级信息
const classInfo = ref({
  className: ''
})

// 知识点列表
const knowledgePoints = ref([
  {
    id: 1,
    title: 'Vue组件通信方式',
    content: 'Vue组件间通信主要有以下几种方式：props、$emit、vuex/pinia、provide/inject、eventBus、$parent/$children等。根据不同的使用场景选择合适的通信方式。',
    tags: ['Vue', '组件', '前端'],
    createTime: '2023-10-15T08:30:00'
  },
  {
    id: 2,
    title: 'HTTP请求方法',
    content: '常见的HTTP请求方法包括GET、POST、PUT、DELETE、PATCH等，分别对应查询、创建、更新、删除、部分更新等操作。',
    tags: ['HTTP', '网络', '后端'],
    createTime: '2023-10-16T14:20:00'
  }
])

// 弹窗状态
const showAddKnowledgePointDialog = ref(false)

// 当前编辑的知识点
const currentKnowledgePoint = reactive({
  id: null,
  title: '',
  content: '',
  tags: []
})

// 标签输入框
const currentTags = ref('')

// 表单验证规则
const formRules = {
  title: [
    { required: true, message: '请输入知识点标题', trigger: 'blur' },
    { min: 2, max: 50, message: '标题长度在2到50个字符之间', trigger: 'blur' }
  ],
  content: [
    { required: true, message: '请输入知识点内容', trigger: 'blur' },
    { min: 10, message: '内容长度不能少于10个字符', trigger: 'blur' }
  ]
}

// 获取班级信息
const getClassInfo = () => {
  // 实际项目中这里会调用API获取班级信息
  classInfo.value = {
    className: '高级前端开发班'
  }
}

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 打开添加弹窗
const openAddDialog = () => {
  currentKnowledgePoint.id = null
  currentKnowledgePoint.title = ''
  currentKnowledgePoint.content = ''
  currentKnowledgePoint.tags = []
  currentTags.value = ''
  showAddKnowledgePointDialog.value = true
}

// 编辑知识点
const handleEdit = (point) => {
  currentKnowledgePoint.id = point.id
  currentKnowledgePoint.title = point.title
  currentKnowledgePoint.content = point.content
  currentKnowledgePoint.tags = [...point.tags]
  currentTags.value = point.tags.join(',')
  showAddKnowledgePointDialog.value = true
}

// 删除知识点
const handleDelete = (id) => {
  ElMessageBox.confirm(
    '确定要删除这个知识点吗？',
    '删除确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    knowledgePoints.value = knowledgePoints.value.filter(point => point.id !== id)
    ElMessage.success('知识点已删除')
  }).catch(() => {
    // 取消删除
  })
}

// 查看详情
const handleViewDetails = (point) => {
  // 实际项目中可能跳转到详情页
  ElMessage.info(`查看知识点"${point.title}"的详情`)
}

// 保存知识点
const saveKnowledgePoint = () => {
  // 处理标签
  currentKnowledgePoint.tags = currentTags.value
    ? currentTags.value.split(',').map(tag => tag.trim()).filter(Boolean)
    : []

  if (currentKnowledgePoint.id) {
    // 更新现有知识点
    const index = knowledgePoints.value.findIndex(p => p.id === currentKnowledgePoint.id)
    if (index !== -1) {
      knowledgePoints.value[index] = { ...currentKnowledgePoint }
      ElMessage.success('知识点已更新')
    }
  } else {
    // 添加新知识点
    const newPoint = {
      ...currentKnowledgePoint,
      id: Date.now(),
      createTime: new Date().toISOString()
    }
    knowledgePoints.value.unshift(newPoint)
    ElMessage.success('知识点发布成功')
  }

  showAddKnowledgePointDialog.value = false
}

// 关闭弹窗
const handleDialogClose = () => {
  showAddKnowledgePointDialog.value = false
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 页面加载时获取数据
onMounted(() => {
  getClassInfo()
})
</script>

<style scoped>
/* 可以添加一些组件特定的样式 */
</style>